package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public PageDTO<BrandDTO> querypage(Integer page, Integer rows,String key) {
        //执行分页查询条件
        Page pagequery = new Page(page, rows);

        //不为空判断
        boolean condition = StringUtils.isNotBlank(key);

        //select * from tb_brand where key=#{key} limit 0,5
        page(pagequery,new QueryWrapper<Brand>()
                //大于两个条件判断优先数时用and（）连接or放中间
            .like(condition, "name", key)
            .or()
            .eq(condition, "letter", key)
        );
        //把分页查询的结果封装到pageDTO中
        return new PageDTO<BrandDTO>(pagequery.getTotal(),pagequery.getPages(),BrandDTO.convertEntityList(pagequery.getRecords()));
    }

    @Override
    @Transactional
    public void insertbrand(BrandDTO brandDTO) {
        //项目一的实现方法也挺不错的
        //DTO先转化为实体对象
        Brand brand = brandDTO.toEntity(Brand.class);
            this.save(brand);
        //关联表实现方式
        //根据分类id集合构造分类品牌中间表的对象，随后批量保存
        ArrayList<CategoryBrand> categorybrands = new ArrayList<>();
        for (Long categoryId : brandDTO.getCategoryIds()) {
            categorybrands.add(CategoryBrand.of(categoryId,brand.getId()));
        }
        categoryBrandService.saveBatch(categorybrands);
    }




    @Override
    @Transactional
    public void updatebrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        this.updateById(brand);
        //删除中间表中修改的信息
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", brand.getId()));

        //根据分类id集合构造分类品牌中间表的对象，随后批量保存
        ArrayList<CategoryBrand> categorybrands = new ArrayList<>();
        for (Long categoryId : brandDTO.getCategoryIds()) {
            categorybrands.add(CategoryBrand.of(categoryId,brand.getId()));
        }
        categoryBrandService.saveBatch(categorybrands);
    }

    @Override
    public List<BrandDTO> querybrandlist(List<Long> ids) {

        return BrandDTO.convertEntityList(listByIds(ids));
    }

    @Override
    public BrandDTO querybrandById(Long id) {
        return new BrandDTO(getById(id));

    }

    @Override
    @Transactional
    public void deleteById(Long bid) {
        this.removeById(bid);
        //根据品牌id删除中间表中的id
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", bid));
    }

    @Override
    public List<BrandDTO> queryBrandByCategory(Long cid) {
        List<Brand> list = getBaseMapper().queryByCategoryId(cid);
        return BrandDTO.convertEntityList(list);
    }


}
